//
//  DDVisitorViewControllor.swift
//  swift微博
//
//  Created by whitcast on 16/7/9.
//  Copyright © 2016年 whitcast. All rights reserved.
//

import UIKit

class DDVisitorViewControllor: UITableViewController {

    //判断是否登录  默认没有
    var isLogin :Bool = DDUserAccountViewModel.sharedUserAccountVM.isLogin ?? false
    //    var isLogin : Bool = true
    //主视图
    var visitorView1 :DDVisitorView?
    
    override func loadView() {//重写loadview,自定义的视图在这个方法里写,这里写,是给定义了初始视图,若是写在viewdidload中,相当于修改了视图,性能降低
        
        super.loadView()
        
        chooseView()
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        print(isLogin)
        
    }

//根据登录状态,选择进入的视图
    func chooseView() {
        if isLogin {
            //登录状态
            
            
        } else {
            //不是登录状态
            visitorView1 = DDVisitorView()
            
            visitorView1?.frame = view.frame
            
            view = visitorView1
            
            visitorView1?.loginCallback = {[weak self] in
                
                self!.loginBtn()
            }
            visitorView1?.registerCallback = {[weak self] in
                
                self!.registerBtn()
            }
            
            view.backgroundColor = UIColor(white: 237.0/255.0, alpha: 1)
            //注册按钮
            navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action:"registerBtn")
            //登录按钮
            navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "登录", style: UIBarButtonItemStyle.Plain, target: self, action: "loginBtn")

        }

    }
   
    
//按钮的点击事件 若是私有的,需要添加@objc,不然崩溃
   @objc private func registerBtn() {
    
    let nav = UINavigationController.init(rootViewController: DDLoginVC())
    
      presentViewController(nav, animated: true, completion: nil)
        
    }
    
   @objc private func loginBtn() {
    
    let nav = UINavigationController.init(rootViewController: DDLoginVC())
    
    presentViewController(nav, animated: true, completion: nil)
    
    }
    
   }
